﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace TimViec.NTD
{
    public partial class ListCongViec : System.Web.UI.Page
    {
        public int nganhngheid { get; set; }
        public int trangthaiid { get; set; }
        public int trang { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["NTD"] == null)
            {
                Response.Redirect("/nha-tuyen-dung.html");
                return;
            }
            if (Request.QueryString["cate"] != null)
            {
                nganhngheid = int.Parse(Request.QueryString["cate"]);
            }
            else
            {
                nganhngheid = -1;
            }
            if (Request.QueryString["status"] != null)
            {
                trangthaiid = int.Parse(Request.QueryString["status"]);
            }
            else
            {
                trangthaiid = -1;
            }
            if (Request.QueryString["page"] != null)
            {
                trang = int.Parse(Request.QueryString["page"]);
            }
            else
            {
                trang = 1;
            }
            if(!IsPostBack)
            {
                LoadNganhNghe();                                
                LoadData(nganhngheid,trangthaiid);
            }
        }
        public void LoadNganhNghe()
        {
            ListItem item = new ListItem();
            item.Text = "--Chọn ngành nghề--";
            item.Selected = true;
            item.Value = "-1";
            droNganhNghe.DataSource = BLL.Admin.NganhNgheHelper.List_NganhNghe_ValueConfig(0);
            droNganhNghe.DataTextField = "Ten";
            droNganhNghe.DataValueField = "NganhNgheId";
            droNganhNghe.DataBind();
            droNganhNghe.Items.Add(item);
        }
        public void LoadData(int nganhNgheid, int TrangThaiid)
        {
            string path = HttpContext.Current.Request.Url.AbsolutePath;
            DAL.UserTuyenDung user = BLL.TuyenDung.UserHelper.Get_UserEmail(Session["NTD"].ToString());
            int page = 1;
            droNganhNghe.SelectedValue = nganhNgheid.ToString();
            droTrangThai.SelectedValue = trangthaiid.ToString();
            if(Request.QueryString["page"]!=null)
            {
                page = int.Parse(Request.QueryString["page"]);
            }            
            rptlistCV.DataSource = BLL.TuyenDung.DangTuyenHelper.List_TinTuyenDungCuaToi(user.UserId, nganhNgheid,
                                                                                         trangthaiid, page, 10);
            rptlistCV.DataBind();
            int Count = BLL.TuyenDung.DangTuyenHelper.CountList_TinTuyenDungCuaToi(user.UserId, nganhNgheid, trangthaiid);
            int TotalPage = Count / 10 + 1;
            if(Count <1)
            {
                lbtPreview.Visible = false;
                lbtNext.Visible = false;                
            }
            if(page==1)
            {
                lbtPreview.Visible = false;
            }
            if (page == TotalPage)
            {
                lbtNext.Visible = false;
            }
            for (int i = page - 4; i < page + 4; i++)
            {
                if (i < 1)
                    continue;
                if (i > TotalPage)
                    continue;
                if (i == page && Count > 0)
                    ltlPaging.Text += "<li><a class='current' href='" + path + "?cate="+nganhNgheid+"&status="+trangthaiid+"&page=" + i + "'>" + i + "</a></li>";
                else
                    ltlPaging.Text += "<li><a href='" + path + "?cate=" + nganhNgheid + "&status=" + trangthaiid + "&page=" + i + "'>" + i + "</a></li>";
            }
        }

        protected void lbtPreview_Click(object sender, EventArgs e)
        {
            string path = HttpContext.Current.Request.Url.AbsolutePath;
            Response.Redirect(path + "?cate=" + nganhngheid + "&status=" + trangthaiid + "&page=" + (trang - 1));
        }

        protected void lbtNext_Click(object sender, EventArgs e)
        {
            string path = HttpContext.Current.Request.Url.AbsolutePath;
            Response.Redirect(path + "?cate=" + nganhngheid + "&status=" + trangthaiid + "&page=" + (trang + 1));
        }

        protected void btSearch_Click(object sender, EventArgs e)
        {
            string path = HttpContext.Current.Request.Url.AbsolutePath;
            Response.Redirect(path + "?cate=" + droNganhNghe.SelectedValue + "&status=" + droTrangThai.SelectedValue + "&page=1");
        }

        protected void btDangViecLam_Click(object sender, EventArgs e)
        {
            Response.Redirect("/nha-tuyen-dung/dang-tuyen-dung.html");
        }

        protected void btActive_Click(object sender, EventArgs e)
        {
            string path = HttpContext.Current.Request.Url.AbsolutePath;
            foreach (RepeaterItem item in rptlistCV.Items)
            {
                HiddenField hiTuyenDungId = item.FindControl("hiTuyenDungId") as HiddenField;
                CheckBox checkList = item.FindControl("checkList") as CheckBox;
                if (checkList.Checked == true)
                {
                    BLL.TuyenDung.DangTuyenHelper.Update_TinhTrang(int.Parse(hiTuyenDungId.Value),2);
                }
            }
            Response.Redirect(path + "?cate=" + droNganhNghe.SelectedValue + "&status=" + droTrangThai.SelectedValue + "&page=1");
        }

        protected void btDelete_Click(object sender, EventArgs e)
        {
            string path = HttpContext.Current.Request.Url.AbsolutePath;
            foreach (RepeaterItem item in rptlistCV.Items)
            {
                HiddenField hiTuyenDungId = item.FindControl("hiTuyenDungId") as HiddenField;
                CheckBox checkList = item.FindControl("checkList") as CheckBox;
                if(checkList.Checked==true)
                {
                    BLL.TuyenDung.DangTuyenHelper.Delete_TinTuyenDung(int.Parse(hiTuyenDungId.Value));
                }
            }
            Response.Redirect(path + "?cate=" + droNganhNghe.SelectedValue + "&status=" + droTrangThai.SelectedValue + "&page=1");
        }

        protected void rptlistCV_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if(e.CommandName=="Edit")
            {
                int tuyenDungId = int.Parse(e.CommandArgument.ToString());
                DAL.tbl_TD_TuyenDung tuyenDung =BLL.TuyenDung.DangTuyenHelper.Get_TinTuyenDung(tuyenDungId);
                Response.Redirect("/nha-tuyen-dung/" + tuyenDungId + "dang-tuyen-dung-buoc"+(tuyenDung.Step +1)+".html");
            }
        }
        
    }
}